#pragma once

#include <iostream>
#include <unordered_map>
#include <string>
#include <fstream>
using namespace std;

namespace dict_ns
{
    string defaultpath = "./dict.txt";
    const string sep = ": ";
    class Dict
    {
    private:
        bool Load()
        {
            cout << _dict_conf_filepath << endl;
            ifstream in(_dict_conf_filepath);//打开文件
            if(in.is_open() == 0)
            {
                cout << "open fail" << endl;
                return false;
            }
            string line;
            while(getline(in,line))
            {
                if(line.empty()) continue;
                auto pos = line.find(sep);
                if(pos == string::npos) continue;
                string word = line.substr(0,pos);
                if(word.empty()) continue;
                string han = line.substr(pos+sep.size());
                if(han.empty()) continue;

                cout << "解析出来的结果: " << word << " " << han << endl; 
                _dict.insert(make_pair(word,han));
            }
            cout << "解析完成" << endl;
            in.close();//关闭文件
            return true;
        }
    public:
        Dict(const string& path = defaultpath):_dict_conf_filepath(path)
        {
            Load();
            bool ret = _dict.empty();

            cout << "jieguo" << ret << endl;
        }

        string translate(const string& word,bool& ok)
        {
            ok = true;
            auto lter = _dict.find(word);
            if(lter == _dict.end())
            {
                ok = false;
                return "未找到";
            }
            return lter->second;
        }

        ~Dict()
        {

        }
    private:
        unordered_map<string,string> _dict;
        string _dict_conf_filepath;
    };

}
